﻿namespace RPGMapa.Controls
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    using RPGMapa.Data;

    #region Delegates

    public delegate void onMouseClick(object sender, MouseEventArgs mea);

    public delegate void onMouseDown(object sender, MouseEventArgs mea);

    public delegate void onMouseMove(object sender, MouseEventArgs mea);

    public delegate void onMouseUp(object sender, MouseEventArgs mea);

    #endregion Delegates

    public partial class MapArea : UserControl
    {
        #region Fields

        MapPainter mp;

        #endregion Fields

        #region Constructors

        public MapArea()
        {
            InitializeComponent();
            this.BackColor = Color.Gray;

            vScrollBar1.Enabled = false;
            hScrollBar1.Enabled = false;
        }

        #endregion Constructors

        #region Events

        public event onMouseClick MAP_MouseClick;

        public event onMouseDown MAP_MouseDown;

        public event onMouseMove MAP_MouseMove;

        public event onMouseUp MAP_MouseUp;

        #endregion Events

        #region Properties

        public MapPainter Map
        {
            get { return mp; }
            set { mp = value; }
        }

        #endregion Properties

        #region Methods

        public void Create(Map map)
        {
            mp = new MapPainter();
            mp.Name = "ActiveMap";
            mp.Create(map);
            mp.Location = new Point(0, 0);
            mp.Size = new Size(mp.TileSize.X * mp.TileCapacity.X,
                               mp.TileSize.Y * mp.TileCapacity.Y);

            // Aqui ele coloca no evento interno o evento externo, ai quando o
            // map (mp) chama seus eventos internos de mouse, ele vai chamar os externos de mouse.
            mp.MouseClick += new MouseEventHandler(
                delegate(object s, MouseEventArgs e)
                {
                    MAP_MouseClick(s, e);
                }
                );
            mp.MouseMove += new MouseEventHandler(
                delegate(object s, MouseEventArgs e)
                {
                    Map_Move(s, e);
                    MAP_MouseMove(s, e);
                }
                );
            mp.MouseDown += new MouseEventHandler(
                delegate(object s, MouseEventArgs e)
                {
                    statusLabel.Text = "Down";
                    MAP_MouseDown(s, e);
                }
                );
            mp.MouseUp += new MouseEventHandler(
                delegate(object s, MouseEventArgs e)
                {
                    statusLabel.Text = "up";
                    MAP_MouseUp(s, e);
                }
                );


            mapContainer.Controls.RemoveByKey("ActiveMap");
            mapContainer.Controls.Add(mp);
        }

        private void hScrollBar1_ValueChanged(object sender, EventArgs e)
        {
            mp.Location = new Point(-hScrollBar1.Value, mp.Location.Y);
        }

        private void Map_Move(object sender, MouseEventArgs e)
        {
            Size s = new Size(
                mp.Width / mp.TileCapacity.X,
                mp.Height / mp.TileCapacity.Y);
            Vector2 position = new Vector2
                ((e.X - e.X % s.Width) / s.Width,
                 (e.Y - e.Y % s.Height) / s.Height);

            statusLabel.Text = "Tile: " + (position.X + 1).ToString() + ", " + (position.Y + 1).ToString();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            mp.Size = new Size(mp.Width * 2, mp.Height * 2);
            if (mp.Width > hScrollBar1.Width)
            {
                hScrollBar1.Maximum = mp.Width - hScrollBar1.Width;
                hScrollBar1.Enabled = true;
            }
            else
            {
                hScrollBar1.Enabled = false;
            }
            if (mp.Height > vScrollBar1.Height)
            {
                vScrollBar1.Maximum = mp.Height - vScrollBar1.Height;
                vScrollBar1.Enabled = true;
            }
            else
            {
                vScrollBar1.Enabled = false;
            }
            mp.Invalidate();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            mp.Size = new Size(mp.Width / 2, mp.Height / 2);
            if (mp.Width > hScrollBar1.Width)
            {
                hScrollBar1.Maximum = mp.Width - hScrollBar1.Width;
                hScrollBar1.Enabled = true;
            }
            else
            {
                hScrollBar1.Value = 0;
                hScrollBar1.Enabled = false;
            }
            if (mp.Height > vScrollBar1.Height)
            {
                vScrollBar1.Maximum = mp.Height - vScrollBar1.Height;
                vScrollBar1.Enabled = true;
            }
            else
            {
                vScrollBar1.Value = 0;
                vScrollBar1.Enabled = false;
            }
            mp.Invalidate();
        }

        private void vScrollBar1_ValueChanged(object sender, EventArgs e)
        {
            mp.Location = new Point(mp.Location.X, -vScrollBar1.Value);
        }

        #endregion Methods
    }
}